// @version=5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ©s3raphic

indicator("Fisher+ [OSC]", "Fisher+ [OSC]", overlay=false, timeframe="", timeframe_gaps=false)



///////////////////////////////////////         Display         ///////////////////////////////////////
in_disp_Fisher_col = input.color(color.white, "Fisher+ Line", group="Fisher+ Interface")
in_disp_bull_col = input.color(color.new(#00bcd4, 0), "Bullish Color", group="Fisher+ Interface")
in_disp_bear_col = input.color(color.new(#e65100, 0), "Bearish Color", group="Fisher+ Interface")
in_disp_level_col = input.color(color.new(#d1d4dc, 0), title="Level Lines Color", group="Fisher+ Interface")

len = input.int(14, minval=1, title='Fisher+ Length', group='Fisher+ Interface')
///////////////////////////////////////         Display         /////////////////////////////////////// 



///////////////////////////////////////         Fisher+         /////////////////////////////////////// 
high_ = ta.highest(hl2, len)
low_ = ta.lowest(hl2, len)

round_(val) =>
    val > 0.99 ? 0.999 : val < -0.99 ? -0.999 : val

value = 0.0
value := round_(0.66 * ((hl2 - low_) / math.max(high_ - low_, 0.001) - 0.5) + 0.67 * nz(value[1]))

fish1 = 0.0
fish1 := 0.5 * math.log((1 + value) / math.max(1 - value, 0.001)) + 0.5 * nz(fish1[1])

fish2 = fish1[1]
///////////////////////////////////////         Fisher+         ///////////////////////////////////////



///////////////////////////////////////         MA Filter         ///////////////////////////////////////
maingroup = "MA Filter Interface"

bandswitch = false
switchcolor = true
bullcolor2 = input.color(color.new(#00bcd4, 0), "Bull Line", group=maingroup)
bearcolor2 = input.color(color.new(#e65100, 0), "Bear Line", group=maingroup)
bullcolor = input.color(color.new(#00bcd4, 70), "Bull Shadow", group=maingroup)
bearcolor = input.color(color.new(#e65100, 70), "Bear Shadow", group=maingroup)
defcolor = color.new(#ffffff, 0)

length = input.int(55, minval=1, title='MA Filter Length', group='MA Filter Interface')
src = fish1
hulloff = 2

hullma = ta.wma(2*ta.wma(src, length/2)-ta.wma(src, length), math.floor(math.sqrt(length)))
hullma2 = hullma[hulloff]
hullmaline = ta.wma(2*ta.wma(src, length/2)-ta.wma(src, length), math.floor(math.sqrt(length)))
hullma2line = hullma[hulloff]
hullcolor = switchcolor ? (hullma > hullma2 ? bullcolor : bearcolor) : defcolor
hullcolor2 = switchcolor ? (((hullmaline > hullma2line) and (hullmaline < fish1)) or ((hullmaline < hullma2line) and (hullmaline < fish1))  ? bullcolor2 : bearcolor2) : defcolor
///////////////////////////////////////         MA Filter         ///////////////////////////////////////



///////////////////////////////////////         Levels         ///////////////////////////////////////
h4 = hline(4, color=color.new(in_disp_bear_col, 65), linestyle=hline.style_solid, linewidth=2, title="4.0 Level", editable=false)
h2 = hline(2.5, color=color.new(in_disp_level_col, 0), linestyle=hline.style_solid, linewidth=1, title="2.0 Level", editable=false)
h0 = hline(0, color=color.new(in_disp_level_col, 50), linestyle=hline.style_dashed, linewidth=1, title="0 Level", editable=false)
h02 = hline(-2.5, color=color.new(in_disp_level_col, 0), linestyle=hline.style_solid, linewidth=1, title="-2.0 Level", editable=false)
h04 = hline(-4, color=color.new(in_disp_bull_col, 65), linestyle=hline.style_solid, linewidth=2, title="-4.0 Level", editable=false)

macolor = switchcolor ? (fish1 > 2.5 ? color.new(in_disp_bear_col, 65) : color.new(#000000, 100)) : in_disp_bull_col
macolor2 = switchcolor ? (fish1 < -2.5 ? color.new(in_disp_bull_col, 65) : color.new(#000000, 100)) : in_disp_bear_col

overbought = (fish1 > 4)
oversold = (fish1 < -4)

plot(fish1, color=in_disp_Fisher_col, linewidth=1, title='Fisher+ Line', editable=false)
plotshape(overbought, color=in_disp_bear_col, style=shape.circle, size=size.tiny, location=location.top, editable = false)
plotshape(oversold, color=in_disp_bull_col, style=shape.circle, size=size.tiny, location=location.bottom, editable = false)

fill(h2, h4, color=macolor, title="Upper Fill", editable=false)
fill(h02, h04, color=macolor2, title="Lower Fill", editable=false)

plot(hullma, color=hullcolor2, linewidth=1, title="HMA2", editable = false)
plot(hullmaline, color=hullcolor, linewidth=9, title="HMA1", editable = false)
///////////////////////////////////////         Levels         ///////////////////////////////////////



///////////////////////////////////////         MACD         ///////////////////////////////////////
sr = close
slength = 12
llength = 26
sigLength = 9

ma(s, l) =>
    ta.ema(s, l)
sema = ma(sr, slength)
lema = ma(sr, llength)
i1 = sema + ma(sr - sema, slength)
i2 = lema + ma(sr - lema, llength)
macdl = i1 - i2
macd = sema - lema
macdsignal = ta.sma(macd, sigLength)

crossover = (macdl > macd) and (macdl > macdsignal) and (macd > macdsignal) and (ta.crossover(macdl, 0))
crossunder = (macdl < macd) and (macdl < macdsignal) and (macd < macdsignal) and (ta.crossunder(macdl, 0))

plotshape(crossover, color=in_disp_bull_col, style=shape.triangleup, size=size.tiny, location=location.bottom, editable = false)
plotshape(crossunder, color=in_disp_bear_col, style=shape.triangledown, size=size.tiny, location=location.top, editable = false)
///////////////////////////////////////         MACD         ///////////////////////////////////////



///////////////////////////////////////         Divergence         ///////////////////////////////////////
div_src_fish = "Fisher+", div_src_trig = "Trigger"
div_gr_name = "Divergence Interface"

in_div_src = input.string(div_src_fish, "Divergence Source", [div_src_fish, div_src_trig], group=div_gr_name)
in_div_lb = 1 //pivot lookback left
in_div_rb = 1 //pivot lookback right
in_div_range_upper = 60 //max lookback range
in_div_range_lower = 1 //min lookback range
in_div_plotBull = input(true, "Plot Bullish", group=div_gr_name)
in_div_plotHiddenBull = input(false, "Plot Hidden Bullish", group=div_gr_name)
in_div_plotBear = input(true, "Plot Bearish", group=div_gr_name)
in_div_plotHiddenBear = input(false, "Plot Hidden Bearish", group=div_gr_name)

in_div_bearColor = input.color(color.new(#e65100, 0), "Bear", inline="DIVC", group=div_gr_name)
in_div_bullColor = input.color(color.new(#00bcd4, 0), "Bull", inline="DIVC", group=div_gr_name)
in_div_textColor = input.color(color.new(#fef5e3, 0), "Text", inline="DIVC", group=div_gr_name)

div_noneColor = color.new(color.white, 100)

f_in_range(cond) =>
	bars = ta.barssince(cond == true)
	in_div_range_lower <= bars and bars <= in_div_range_upper

div_osc = switch in_div_src
    div_src_fish => fish1
    div_src_trig => fish2

div_pl_found = na(ta.pivotlow(div_osc, in_div_lb, in_div_rb)) ? false : true
div_ph_found = na(ta.pivothigh(div_osc, in_div_lb, in_div_rb)) ? false : true

//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
div_oscHL = div_osc[in_div_rb] > ta.valuewhen(div_pl_found, div_osc[in_div_rb], 1) and f_in_range(div_pl_found[1])

// Price: Lower Low
div_priceLL = low[in_div_rb] < ta.valuewhen(div_pl_found, low[in_div_rb], 1)
div_bullCond = in_div_plotBull and div_priceLL and div_oscHL and div_pl_found

plot(div_pl_found ? div_osc[in_div_rb] : na, offset=-in_div_rb, title="Regular Bullish", linewidth=2, color=(div_bullCond ? in_div_bullColor : div_noneColor))
plotshape(div_bullCond ? div_osc[in_div_rb] : na, offset=-in_div_rb, title="Regular Bullish Label", text="Bull", style=shape.labelup, location=location.absolute, color=in_div_bullColor, textcolor=in_div_textColor)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
div_oscLL = div_osc[in_div_rb] < ta.valuewhen(div_pl_found, div_osc[in_div_rb], 1) and f_in_range(div_pl_found[1])

// Price: Higher Low
div_priceHL = low[in_div_rb] > ta.valuewhen(div_pl_found, low[in_div_rb], 1)
div_hiddenBullCond = in_div_plotHiddenBull and div_priceHL and div_oscLL and div_pl_found

plot(div_pl_found ? div_osc[in_div_rb] : na, offset=-in_div_rb, title="Hidden Bullish", linewidth=2, color=(div_hiddenBullCond ? color.new(in_div_bullColor, 100) : div_noneColor))
plotshape(div_hiddenBullCond ? div_osc[in_div_rb] : na, offset=-in_div_rb, title="Hidden Bullish Label", text=" H Bull ", style=shape.labelup, location=location.absolute, color=in_div_bullColor, textcolor=in_div_textColor)

//------------------------------------------------------------------------------
// Regular Bearish
// Osc: Lower High
div_oscLH = div_osc[in_div_rb] < ta.valuewhen(div_ph_found, div_osc[in_div_rb], 1) and f_in_range(div_ph_found[1])

// Price: Higher High
div_priceHH = high[in_div_rb] > ta.valuewhen(div_ph_found, high[in_div_rb], 1)
div_bearCond = in_div_plotBear and div_priceHH and div_oscLH and div_ph_found

plot(div_ph_found ? div_osc[in_div_rb] : na, offset=-in_div_rb, title="Regular Bearish", linewidth=2, color=(div_bearCond ? in_div_bearColor : div_noneColor))
plotshape(div_bearCond ? div_osc[in_div_rb] : na, offset=-in_div_rb, title="Regular Bearish Label", text=" Bear ", style=shape.labeldown, location=location.absolute, color=in_div_bearColor, textcolor=in_div_textColor)

//------------------------------------------------------------------------------
// Hidden Bearish
// Osc: Higher High
div_oscHH = div_osc[in_div_rb] > ta.valuewhen(div_ph_found, div_osc[in_div_rb], 1) and f_in_range(div_ph_found[1])

// Price: Lower High
div_priceLH = high[in_div_rb] < ta.valuewhen(div_ph_found, high[in_div_rb], 1)
div_hiddenBearCond = in_div_plotHiddenBear and div_priceLH and div_oscHH and div_ph_found

plot(div_ph_found ? div_osc[in_div_rb] : na, offset=-in_div_rb, title="Hidden Bearish", linewidth=2, color=(div_hiddenBearCond ? color.new(in_div_bearColor, 100) : div_noneColor))
plotshape(div_hiddenBearCond ? div_osc[in_div_rb] : na, offset=-in_div_rb, title="Hidden Bearish Label", text=" H Bear ", style=shape.labeldown, location=location.absolute, color=in_div_bearColor, textcolor=in_div_textColor)
///////////////////////////////////////         Divergence         ///////////////////////////////////////



///////////////////////////////////////         Alerts         ///////////////////////////////////////
alertcondition(overbought, title = "Fisher+ Extreme Overbought", message = "Fisher+ Extreme Overbought has Occured")
alertcondition(oversold, title = "Fisher+ Extreme Oversold", message = "Fisher+ Extreme Oversold has Occured")
alertcondition(crossover, title = "Fisher+ Extreme Overbought", message = "Fisher+ Extreme Overbought has Occured")
alertcondition(crossunder, title = "Fisher+", message = "Fisher+ Extreme Oversold has Occured")
alertcondition(div_bullCond, title = "Fisher+ Bullish Divergence", message = "Fisher+ Bullish Divergence has Occured")
alertcondition(div_bearCond, title = "Fisher+ Bearish Divergence", message = "Fisher+ Bearish Divergence has Occured")
alertcondition(div_hiddenBullCond, title = "Fisher+ Hidden Bullish Divergence", message = "Fisher+ Hidden Bullish Divergence has Occured")
alertcondition(div_hiddenBearCond, title = "Fisher+ Hidden Bearish Divergence", message = "Fisher+ Hidden Bearish Divergence has Occured")
///////////////////////////////////////         Alerts         ///////////////////////////////////////
